package letsjava.db;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

import letsjava.pattern.flyweight.ConcreteFlyweight;
import letsjava.pattern.flyweight.Flyweight;

public class NSTable {
	private String dbName;
	private String tableName;
	private NSDB db;
	
	static Hashtable flyweights = new Hashtable();

	public static NSTable getTable(String dbName, String tableName) {
		NSTable flyweight = (NSTable) flyweights.get(dbName);
		if (flyweight == null) {
			// 产生新的ConcreteFlyweight
			flyweight = new NSTable(dbName,tableName);
			flyweights.put(dbName, flyweight);
		}
		return flyweight;
	}

	public static void main(String[] args) throws SQLException {
		NSTable db = new NSTable("test", "student");
		HashMap<String, String> line = new HashMap<String, String>();
		line.put("SNO", "3317");
		line.put("SNAME", "ggggt");
		line.put("sex", "m");
		line.put("BDATE", "1981-01-01");
		//db.add(line);
		String[] pp = {"1"};
		ArrayList a = new ArrayList();
		a.add("2");
;		System.out.println(db.getDB().queryBind("select * from test where id = ?",pp));
	}



	public NSTable(String dbName, String tableName) {
		this.dbName = dbName;
		this.tableName = tableName;
		connect();
	}

	public NSTable(String tableName) {
		this.dbName = "test";
		this.tableName = tableName;
		connect();
	}

	public ArrayList<HashMap> query(String sql) throws SQLException {
		return db.query(sql);
	}
	
	public void exec(String sql) throws SQLException {
		db.exec(sql);
	}

	public void save(HashMap a) throws SQLException {
		if (a.get("id") == null) {
			add(a);
		} else {
			HashMap condition = new HashMap();
			condition.put("id", a.get("id"));
			a.remove("id");
			update(a, condition);
		}
	}

	public void add(HashMap<String, String> a) throws SQLException {
		ArrayList<String> param = new ArrayList<String>();
		StringBuffer sql = new StringBuffer();
		sql.append("insert into " + tableName + "(");
		for (String f : a.keySet()) {
			sql.append(f + ",");
		}
		sql.deleteCharAt(sql.length() - 1);
		sql.append(") values(");
		for (String f : a.keySet()) {
			sql.append("?,");
			param.add(a.get(f));
		}
		sql.deleteCharAt(sql.length() - 1);
		sql.append(")");

		db.execBind(sql.toString(), param);

	}

	public void update(HashMap<String, String> a, HashMap<String, String> b)
	        throws SQLException {
		ArrayList<String> param = new ArrayList<String>();
		StringBuffer sql = new StringBuffer();
		sql.append("update " + tableName + " set ");
		for (String f : a.keySet()) {
			sql.append(f + "=?,");
			param.add(a.get(f));
		}
		sql.deleteCharAt(sql.length() - 1);
		sql.append(") values(");
		for (String f : a.keySet()) {
			sql.append("?,");
			param.add(a.get(f));
		}
		sql.delete(sql.length() - 1, sql.length());
		sql.append(")");

		db.execBind(sql.toString(), param);
	}

	public void connect() {
		db = new NSDB(dbName);
		NSDBObserver observer = new NSDBObserver();
		db.addObserver(observer);
		db.connect();
	}

	public void close() {
		db.colse();
	}

	public void setDB(NSDB db) {
		this.db = db;
	}

	public NSDB getDB() {
		return db;
	}
}
